package io.renren.modules.carcustomerinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.renren.modules.carcustomerinfo.dto.CarCustomerInfoDTO;
import io.renren.modules.carcustomerinfo.vo.CarCustomerInfoVO;
import io.renren.modules.carinfo.entity.CarInfoEntity;
import io.renren.modules.carinfo.service.CarInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.carcustomerinfo.dao.CarCustomerInfoDao;
import io.renren.modules.carcustomerinfo.entity.CarCustomerInfoEntity;
import io.renren.modules.carcustomerinfo.service.CarCustomerInfoService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("carCustomerInfoService")
public class CarCustomerInfoServiceImpl extends ServiceImpl<CarCustomerInfoDao, CarCustomerInfoEntity> implements CarCustomerInfoService {
    @Autowired
    private CarInfoService carInfoService;
    @Autowired
    private UserService userService;
    @Override
    public PageData<CarCustomerInfoVO> queryCustomerInfoPage(Map<String, Object> params) {
        log.info("客户预约信息表(分页)");
        // 获取信息
        String customerName = (String) params.get("customerName");
        Integer page = Integer.valueOf((String) params.get("page"));
        Integer limit = Integer.valueOf((String) params.get("limit"));
        QueryWrapper<CarCustomerInfoEntity> wrapper = new QueryWrapper<CarCustomerInfoEntity>();
        if (StringUtils.isNotBlank(customerName)) {
            wrapper.like("customer_name",customerName);
        }
        // 预约时间过期不展示
        wrapper.ge("appointment_time",System.currentTimeMillis());
        wrapper.orderByAsc("appointment_time");
        // 创建分页对象
        IPage<CarCustomerInfoEntity> iPage = new Page<CarCustomerInfoEntity>(page,limit);
        this.baseMapper.selectPage(iPage,wrapper);
        // 分页集合
        List<CarCustomerInfoEntity> records = iPage.getRecords();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CarCustomerInfoVO> customerInfoVOS = new ArrayList<CarCustomerInfoVO>();
        for (CarCustomerInfoEntity record : records) {
            Long tbUserId = record.getTbUserId();
            Long carInfoId = record.getCarInfoId();
            CarInfoEntity carInfoEntity = carInfoService.getById(carInfoId);
            CarCustomerInfoVO carCustomerInfoVO = JacksonUtils.obj2pojo(record, CarCustomerInfoVO.class);
            carCustomerInfoVO.setCustomerName(userService.queryByUserId(tbUserId).getCustomerName());
            carCustomerInfoVO.setCustomerIphone(userService.queryByUserId(tbUserId).getMobile());
            carCustomerInfoVO.setAppointmentDate(simpleDateFormat.format(new Date(record.getAppointmentTime())));
            carCustomerInfoVO.setCarModel(carInfoEntity.getCarModel());
            carCustomerInfoVO.setCarNumber(carInfoEntity.getCarNumber());
            carCustomerInfoVO.setEnergyType(carInfoEntity.getEnergyType());
            carCustomerInfoVO.setLevel(carInfoEntity.getLevel());
            customerInfoVOS.add(carCustomerInfoVO);
        }
        // 总数
        long total = iPage.getTotal();
        PageData<CarCustomerInfoVO> pageData = new PageData<CarCustomerInfoVO>(customerInfoVOS,total);
        return pageData;
    }

    @Override
    public void subscribe(CarCustomerInfoDTO dto) {
        log.info("客户预约");
        CarCustomerInfoEntity carCustomerInfoEntity = JacksonUtils.obj2pojo(dto, CarCustomerInfoEntity.class);
        this.baseMapper.insert(carCustomerInfoEntity);
    }

    @Override
    public JSONObject subscribelist(Long userId) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<CarCustomerInfoEntity> wrapper = new QueryWrapper<CarCustomerInfoEntity>();
        wrapper.eq("tb_user_id",userId);
        List<CarCustomerInfoEntity> carCustomerInfoEntities = this.baseMapper.selectList(wrapper);
        for (CarCustomerInfoEntity infoEntity : carCustomerInfoEntities) {
            Long tbUserId = infoEntity.getTbUserId();
            Long carInfoId = infoEntity.getCarInfoId();
            CarInfoEntity carInfoEntity = carInfoService.getById(carInfoId);
            JSONObject json = new JSONObject();
            json.put("customerName",userService.queryByUserId(tbUserId).getCustomerName());
            json.put("customerIphone",userService.queryByUserId(tbUserId).getMobile());
            json.put("carModel",carInfoEntity.getCarModel());
            json.put("appointmentTime",simpleDateFormat.format(new Date(infoEntity.getAppointmentTime())));
            jsonObjects.add(json);
        }

        JSONObject jsonObject = new JSONObject();
        UserEntity userEntity = userService.queryByUserId(userId);
        jsonObject.put("userEntity",userEntity);
        jsonObject.put("carCustomerInfo",jsonObjects);
        return jsonObject;
    }
}
